<% // Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License. %>

<%@page language="java" contentType="text/html; charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="edu.cmu.lti.sepia.util.ServletUtil"%>
<%@page import="edu.cmu.lti.sepia.data.session.SessionManager"%>
<%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%>
<%@page import="edu.cmu.lti.sepia.data.db.*"%>
<%@page import="edu.cmu.lti.sepia.infra.topic.model.Topic"%>
<%@page import="edu.cmu.lti.sepia.infra.topic.model.Nugget"%>
<%@page import="edu.cmu.lti.sepia.util.Logger"%>
<%@page import="edu.cmu.lti.sepia.util.FormatUtil"%>
<%@ page import="edu.cmu.lti.sepia.servlet.Commons"%>

<%
out.println( Commons.getHeader(request, response, "Exporter", 2) );

SQL sql = new SQL();
ServletUtil s = new ServletUtil( request );
String taskNameShort;
if ( request.getParameter("taskNameShort")!=null ) {
	taskNameShort = (String)request.getParameter("taskNameShort");
	SessionManager.storeTask(sql, taskNameShort, session);
} else {
	taskNameShort = (String)session.getAttribute("taskNameShort");
}
String type = (String)request.getParameter("type");
boolean selectedOnly = request.getParameter("selected")!=null;
String srcLang = s.readString("srcLang","");
String trgLang = s.readString("trgLang","");
boolean isMonolingual = s.readBoolean("isMonolingual", false);
int userId = s.readInt("userId", -1);

List<Topic> topics = TopicDAO.getGoldStandard( sql, taskNameShort, userId, null );

Logger.log( sql, session, "EXPORT TOPICS (HTML)", "" );
sql.close();
TopicDAO.removeSimilarNuggets(topics);
TopicDAO.generateExternalIds(topics,trgLang,4);
if (selectedOnly) {
  TopicDAO.removeUnselected(topics);
}
%>
<%!
String printHeader(String type, boolean isMonolingual) {
  StringBuilder sb = new StringBuilder();
  sb.append("<tr>\n");
  sb.append("<th>Internal ID</th>\n");
  sb.append("<th>External ID</th>\n");
  sb.append("<th>Selected</th>\n");
  sb.append("<th>Title</th>\n");
  sb.append("<th>Answer Type</th>\n");
  if (!isMonolingual) { 
    sb.append("<th>Question</th>\n");
  }
  sb.append("<th>Question</th>\n");
  if (!isMonolingual) { 
    sb.append("<th>Narrative</th>\n");
  }
  sb.append("<th>Narrative</th>\n");
  sb.append("<th>Note</th>\n");
  if (type.equals("gs")) {
  sb.append("<th>Answer Nuggets</th>\n");
  }
  sb.append("</tr>\n");
  return sb.toString();
}
%>
<table cellpadding="0" cellspacing="0" border="0" class="datatable">
<thead>
<%= printHeader(type, isMonolingual) %>
</thead>
<tbody>
<%
for (int i=0;i<topics.size();i++){
    Topic topic = topics.get(i);
	out.println("<tr class=\""+(i%2==1?"odd":"even")+"\">");
    out.println("<td nowrap>" + topic.getTopicId() + "</td>");
    out.println("<td nowrap>" + topic.getExternalId() + "</td>");
    out.println("<td nowrap>" + (topic.isDone()?1:0) + "</td>");
    out.println("<td nowrap>" + topic.getTitle() + "</td>");
    out.println("<td nowrap>" + topic.getAnswerType() + "</td>");
    if (!isMonolingual) { 
      out.println("<td nowrap>" + topic.getQuestionSrc() + "</td>");
    }
    out.println("<td nowrap>" + topic.getQuestionTrg() + "</td>");
    if (!isMonolingual) { 
      out.println("<td nowrap>" + topic.getNarrativeSrc() + "</td>");
    }
    out.println("<td nowrap>" + topic.getNarrativeTrg() + "</td>");
    out.println("<td nowrap>" + topic.getNote() + "</td>");
    
    if (type.equals("gs") ) {
      out.print("<td nowrap>" );
      List<Nugget> nuggets = topic.getNuggets();
      if (nuggets!=null) {
        StringBuilder concatenated = new StringBuilder();
        for ( int counter = 0; counter<nuggets.size(); counter++ ) {
          Nugget n = nuggets.get(counter);
          concatenated.append( "["+FormatUtil.nf4(n.getWeight())+" "+n.getText()+"]" );
        }
        out.print(concatenated.toString());
      } else {
        out.print("&nbsp;");
      }
      out.println("</td>" );
    }
    out.println("</tr>");
}
%>
</tbody>
<tfoot>
<%= printHeader(type, isMonolingual) %>
</tfoot>
</table>


<%
out.println( Commons.getFooter(request, response) );
%>